package com.qmyx.supply.admin.controller;

import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.api.ResultCode;
import com.ytjj.qmyx.supplychain.common.model.request.ProductLibraryRequest;
import com.ytjj.qmyx.supplychain.common.model.request.ProductLibrarySpecRequest;
import com.ytjj.qmyx.supplychain.common.model.request.ProductSelectRequest;
import com.ytjj.qmyx.supplychain.common.model.response.ProductLibraryResponse;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProductLibrary;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProductLibrarySpec;
import com.ytjj.qmyx.supplychain.service.BankerService;
import com.ytjj.qmyx.supplychain.service.ProductLibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/productLibrary")
public class ProductLibraryController {
    @Resource
    private ProductLibraryService productLibraryService;
    @Autowired
    private BankerService bankerService;

    /**
     *
     * @param productLibraryRequest
     * @return
     */
    @PostMapping("/selectList")
    public CommonResult productLibraryList(@RequestBody ProductLibraryRequest productLibraryRequest) {
        CommonPage<YxProductLibrary> pageList = productLibraryService.selectList(productLibraryRequest);
        return CommonResult.success(pageList);
    }

    /**
     * @Description 新增商品库
     *
     *
     * 
     */
    @PostMapping("/addProductLibrary")
    //@OperLog(operModule = "商品库模块", operDesc = "新增商品库", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult addProductLibrary(@RequestBody ProductLibraryRequest productLibraryRequest) {
        productLibraryService.addProductLibrary(productLibraryRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * @Description 获取上传商品附件链接
     *
     *
     * 
     */
    @GetMapping("/getZipUrl")
    public CommonResult getZipUrl(@RequestParam("id") Integer id) {
        return CommonResult.success(productLibraryService.getZipUrl(id));
    }

    @GetMapping("/getCategoryList")
    public CommonResult getTopCategory() {
        return CommonResult.success(productLibraryService.getCategoryList());
    }

    /**
     * 获取一级分类
     *
     * @return
     */
    @GetMapping("/getTopCategoryList")
    public CommonResult getTopCategoryList() {
        return CommonResult.success(productLibraryService.getTopCategoryList());
    }

    /**
     * 获取二级分类
     *
     * @param id
     * @return
     */
    @GetMapping("/getChildCategoryList")
    public CommonResult getChildCategoryList(@RequestParam("id") Integer id) {
        return CommonResult.success(productLibraryService.getChildCategoryList(id));
    }


    /**
     * 根据分类id查询属性集合
     * @param categoryId
     * @return
     */
    @GetMapping(value = "/getSpecListByCategoryId")
    public CommonResult getSpecListByCategoryId(@RequestParam("categoryId") Integer categoryId) {
        List<Map<String,Object>> specLists = productLibraryService.getSpecListByCategoryId(categoryId);
        return CommonResult.success(specLists);
    }


    /**
     * 查询商品规格表列表
     * @param productLibrarySpecRequest
     * @return
     */
    @PostMapping("/getProductSpec")
    public CommonResult getProductSpec(@RequestBody ProductLibrarySpecRequest productLibrarySpecRequest){
        CommonPage<YxProductLibrarySpec> pageList = productLibraryService.getProductSpec(productLibrarySpecRequest);
        return CommonResult.success(pageList);
    }
    /**
     * 添加选品商品
     */
    @PostMapping("/insertProductSelect")
    //@OperLog(operModule = "商品库模块", operDesc = "新增选品商品", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult insertProductSelect(@RequestBody ProductSelectRequest productSelectRequest){
        int count = productLibraryService.insertProductSelect(productSelectRequest);
        if (count > 0){
            return CommonResult.success(count);
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 新增规格属性
     *
     * @param attributeId
     * @return
     */
    @GetMapping("/addAttributeValue")
    public CommonResult addAttributeValue(@RequestParam("attributeId") Integer attributeId,
                                          @RequestParam("value") String value) {
        int count = productLibraryService.insertAttributeValue(attributeId, value);
        if(count > 0) {
            return CommonResult.success(ResultCode.SUCCESS);
        }
        return CommonResult.success(ResultCode.FAILED);
    }

    /**
     * 删除规格属性
     *
     * @param id
     * @return
     */
    @GetMapping("/delAttributeValue")
    public CommonResult delAttributeValue(@RequestParam("id") Integer id) {
        Integer count = productLibraryService.deleteAttributeValue(id);
        if(count > 0) {
            return CommonResult.success(ResultCode.SUCCESS);
        }
        return CommonResult.success(ResultCode.FAILED);
    }

    /**
     * @Description 更新规格库存
     *
     *
     * 
     * @Version 1.0
     **/
    @GetMapping("/updateProductLibraryStock")
    public CommonResult updateProductLibraryStock(@RequestParam("id") Integer id,
                                                  @RequestParam("stock") Integer stock) {
        productLibraryService.updateProductLibraryStock(id, stock);
        return CommonResult.success(ResultCode.SUCCESS);
    }




}
